package coder;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.net.InetSocketAddress;
import java.util.Arrays;

public class TestClient {

    public static void main(String[] args) throws InterruptedException {
        NioEventLoopGroup group = new NioEventLoopGroup();
        Bootstrap boot=new Bootstrap();
                boot
                .group(group)
                .channel(NioSocketChannel.class)
                .handler(new SimpleChannelInboundHandler<ByteBuf>() {
                    @Override
                    public void channelActive(ChannelHandlerContext ctx) throws Exception {
                        System.out.println("链接成功");
                    }

                    @Override
                    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
                        cause.getCause().printStackTrace();
                        ctx.close();
                    }

                    @Override
                    protected void channelRead0(ChannelHandlerContext ctx, ByteBuf byteBuf) throws Exception {
//                        byte[] bytes = NumberConvertByte.IntToByteArray(66);
//                        ByteBuf byteBuf1 = ctx.alloc().buffer();
//                        byteBuf1.writeByte(bytes[0]);
//                        byteBuf1.writeByte(bytes[1]);
//                        byteBuf1.writeByte(bytes[2]);
//                        byteBuf1.writeByte(bytes[3]);
//                        ctx.writeAndFlush(byteBuf1);
//
//
//                        byte[] bytes2 = NumberConvertByte.IntToByteArray(99);
//                        ByteBuf byteBuf2 = ctx.alloc().buffer();
//                        byteBuf2.writeByte(bytes2[0]);
//                        byteBuf2.writeByte(bytes2[1]);
//
//                        ctx.writeAndFlush(byteBuf2);
//                        Thread.sleep(3000);
//                        ByteBuf byteBuf3 = ctx.alloc().buffer();
//                        byteBuf3.writeByte(bytes2[2]);
//                        byteBuf3.writeByte(bytes2[3]);
//                        ctx.writeAndFlush(byteBuf3);

                        for (int i = 0; i < 20; i++) {
                            byte[] b= NumberConvertByte.IntToByteArray(i);
                            for (Byte aByte : b) {
                                Thread.sleep(200);
                                ByteBuf buffer = ctx.alloc().buffer().writeByte(aByte);
                                ctx.writeAndFlush(buffer);

                                if(i==10){
                                    byte[] bytes = new byte[2048];
                                    ByteBuf byteBuf1 = ctx.alloc().buffer().writeBytes(bytes);
                                    ctx.writeAndFlush(byteBuf1);
                                }

                            }

                        }

                    }
                })
                .remoteAddress(new InetSocketAddress("127.0.0.1",9999));
        try {
            ChannelFuture channelFuture = boot.connect().sync();
            channelFuture.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally{
            group.shutdownGracefully().sync();
        }

    }

}
